package com.yifeng.repo.controller.excel.loader.action;

import com.yifeng.repo.base.utils.converter.JacksonHelper;
import com.yifeng.repo.controller.excel.loader.biz.BizImportAction;
import com.yifeng.repo.controller.excel.loader.dto.DataTaskDto;
import com.yifeng.repo.controller.excel.loader.dto.RowDataDto;
import com.yifeng.repo.controller.excel.loader.dto.RowResultDto;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by daibing on 2024/4/18.
 */
public class GoodsImportAction implements BizImportAction {

    @Override
    public String dataFileUrl() {
        return "https://snap.yifengx.com/YFX1326BqMdXsHyu/goods-center/excel/export/673134472883429376.xlsx";
    }

    @Override
    public Map<Integer, String> templateHeader() {
        Map<Integer, String> header = new LinkedHashMap<>();
        header.put(0, "goods_code");
        header.put(1, "bar_code");
        header.put(2, "goods_desc");
        header.put(3, "category_level_1");
        header.put(4, "category_level_2");
        header.put(5, "category_level_3");
        header.put(6, "category_level_4");
        header.put(7, "tag_key");
        header.put(8, "tag_value");
        header.put(9, "tag_modify_time");
        return header;
    }

    @Override
    public List<RowResultDto> sink(List<RowDataDto> rows) {
        System.out.println("sink rows: " + rows.size() + ", last row number: "
                + (rows.size() > 0 ? rows.get(rows.size() - 1).getRowNumber() : Integer.MAX_VALUE));
        return rows.stream()
                .map(s -> {
                    if (s.getRowNumber() % 10000 == 0) {
                        return new RowResultDto(s.getRowNumber(), s.getData(), false, "import failed!");
                    } else {
                        return new RowResultDto(s.getRowNumber(), s.getData(), true, "import success!");
                    }
                })
                .collect(Collectors.toList());
    }

    @Override
    public String importLogBizHeader() {
        return "商品编码,国际条形码,商品描述,品类一级,品类二级,品类三级,品类四级,标签名称,标签值,标签更新时间,";
    }

    @Override
    public void totalFinished(DataTaskDto dataTaskDto) {
        System.out.println("total finished return: " + JacksonHelper.toJson(dataTaskDto));
        System.exit(0);
    }

    @Override
    public void failed(DataTaskDto dataTaskDto) {
        System.out.println("failed return: " + JacksonHelper.toJson(dataTaskDto));
        System.exit(-1);
    }
}
